import { DB } from "../db"


let t_code_id = "t_tea_code_id"
let t_id_code = "t_tea_id_code"
export namespace TeaCodeID {
	export async function createMap(code:string,id:number) {
		await DB.redis.hset(t_code_id,code,id)
		await DB.redis.hset(t_id_code,id.toString(),code)
		return true 
	}

	export async function delMap(code:string,id:number) {
		await DB.redis.hdel(t_code_id,code)
		await DB.redis.hdel(t_id_code,id + "")
	}

	export async function getCode(id:number) {
		let code:string = await DB.redis.hget(t_id_code,id + "")
		return code 
	}

	export async function getID(code:string) {
		let id:string = await DB.redis.hget(t_code_id,code) 
		if(id) {
			let ret = Number.parseInt(id)
			if(Number.isNaN(ret)) {
				return null
			}
			return ret 
		}
		return null
	}

	export async function toID(codeOrID:string | number) {
		if(typeof(codeOrID) == "string") {
			return await getID(codeOrID)
		}
		return codeOrID
	}

	export async function toCode(codeOrID:string | number) {
		if(typeof(codeOrID) == "number") {
			return await getCode(codeOrID)
		}
		return codeOrID
	}
}